package io.dogstudio.bean;

import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;

import javax.lang.model.element.Modifier;

import io.dogstudio.bean.Student;

/**
 * TODO 类描述
 *
 * @author HB.cby
 * @date 2020-10-27
 **/
class JavapoetTest {

	public static void main(String[] args) {
//		Student student = new Student("zhangsan", 20);
//		System.out.println("student:"+student.getName()+" "+ student.getAge());
//		student.setAge(25);
//		student.setName("lili");
//		System.out.println("student:"+student.getName()+" "+ student.getAge());
		generateStudentBean();
	}


	/**
	 *
	 * package com.example.helloworld;
	 *
	 * public final class HelloWorld {
	 *   public static void main(String[] args) {
	 *     System.out.println("Hello, JavaPoet!");
	 *   }
	 * }
	 *
	 *
	 *
	 */
	private static void generateHelloWorld() {
		MethodSpec methodSpec = MethodSpec.methodBuilder("main")
				.addParameter(String[].class, "args").returns(void.class)
				.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
				.addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!").build();

		TypeSpec typeSpec = TypeSpec.classBuilder("HelloWorld")
				.addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(methodSpec).build();

		JavaFile javaFile = JavaFile.builder("com.example.helloworld", typeSpec).build();

		try {
			javaFile.writeTo(System.out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void generateStudentBean() {
		MethodSpec constructMethod = MethodSpec.constructorBuilder()
				.addModifiers(Modifier.PUBLIC)
				.addParameter(String.class, "name").addParameter(int.class, "age")
				.addStatement("$N.$N = $N", "this", "name", "name")
				.addStatement("$N.$N = $N", "this", "age", "age").build();

		MethodSpec getName = MethodSpec.methodBuilder("getName").returns(String.class)
				.addModifiers(Modifier.PUBLIC)
				.addStatement("return $N", "name").build();
		MethodSpec setName = MethodSpec.methodBuilder("setName").addParameter(String.class, "name")
				.addModifiers(Modifier.PUBLIC)
				.returns(void.class).addStatement("$N.$N = $N", "this", "name", "name").build();

		MethodSpec getAge = MethodSpec.methodBuilder("getAge").returns(int.class)
				.addModifiers(Modifier.PUBLIC)
				.addStatement("return $N", "age").build();

		MethodSpec setAge = MethodSpec.methodBuilder("setAge").addParameter(int.class, "age")
				.addModifiers(Modifier.PUBLIC)
				.returns(void.class).addStatement("$N.$N = $N", "this", "age", "age").build();


		TypeSpec student = TypeSpec.classBuilder("Student")
				.addModifiers(Modifier.PUBLIC, Modifier.FINAL).addSuperinterface(Serializable.class)
				.addField(FieldSpec.builder(String.class, "name", Modifier.PRIVATE).build())
				.addField(FieldSpec.builder(int.class, "age", Modifier.PRIVATE).build())
				.addMethod(constructMethod).addMethod(getName).addMethod(setName).addMethod(getAge)
				.addMethod(setAge).build();

		JavaFile file = JavaFile.builder("io.dogstudio.bean", student).build();
		try {
			File writeFile = new File("D:\\TestGenerate");
			file.writeTo(writeFile);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
